package com.cote.douhahou.pretloan.status;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.cote.douhahou.pretloan.service.CusServiceAkomapalendDialog;
import com.cote.douhahou.pretloan.beans.AppAkomapalendConfigCustomize;
import com.cote.douhahou.pretloan.beans.LnLiAkomapalendmitInfo;
import com.cote.douhahou.pretloan.beans.OtheAkomapalendrsInfo;
import com.cote.douhahou.pretloan.beans.RpIAkomapalendnfo;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;
import com.cote.douhahou.pretloan.R;

public class LnAKomapalendRpyFragment extends BaseAkomapalendLnFragment {

    private TextView homeAmountTv,homeTermTv;
    private TextView totalRepaymentTv,dueDateTv,daysRemainingTv,loanAmountTv,interestTv,serviceFeeTv,vatTv,overdueDaysTv,overduePenaltyTv;
    private View foldView,vatView;
    private ImageButton foldIb;
    private View repayBtn;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_repay, container, false);

        homeAmountTv = view.findViewById(R.id.flra_tv_home_amount);
        homeTermTv = view.findViewById(R.id.flra_tv_home_term);
        totalRepaymentTv = view.findViewById(R.id.flra_tv_total_repayment);
        dueDateTv = view.findViewById(R.id.flra_tv_due_date);
        daysRemainingTv = view.findViewById(R.id.flra_tv_days_remaining);
        loanAmountTv = view.findViewById(R.id.flra_tv_loan_amount);
        interestTv = view.findViewById(R.id.flra_tv_interest);
        serviceFeeTv = view.findViewById(R.id.flra_tv_service_fee);
        vatTv = view.findViewById(R.id.flra_tv_vat);
        overdueDaysTv = view.findViewById(R.id.flra_tv_overdue_days);
        overduePenaltyTv = view.findViewById(R.id.flra_tv_overdue_penalty);
        foldView = view.findViewById(R.id.flra_view_flod);
        vatView = view.findViewById(R.id.flra_view_vat);
        foldIb = view.findViewById(R.id.flra_ib_fold);
        repayBtn = view.findViewById(R.id.flra_btn_repay);

        view.findViewById(R.id.flra_ib_customer_service).setOnClickListener(v -> new CusServiceAkomapalendDialog(requireContext()).show());

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.flra_refreshlayout);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpHelper.inst().submitEvent("REPAY","REFRESH_CLICK","");
            HttpHelper.inst().fRepayDetail(requireActivity(),this::refreshDetails_akomapalend);
        });


        init_akomapalend();

        return view;
    }

    private void init_akomapalend(){
        //折叠/展开
        foldIb.setOnClickListener(v -> fold());

        repayBtn.setOnClickListener(v -> {
            HttpHelper.inst().submitEvent("REPAY","SUBMIT_CLICK","");
            if (AkomapaLendUtil.isRepayType2()) HttpHelper.inst().repayPath1(requireActivity(),"NO",this::toPay_akomapalend);
            else if (AkomapaLendUtil.isRepayType1()) HttpHelper.inst().repayPath2(requireActivity(),"NO",this::toPay_akomapalend);
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        HttpHelper.inst().submitEvent("REPAY","START","");
        HttpHelper.inst().homeAmounts(requireActivity(), this::refreshHomeLimit);
    }

    private void fold(){

        if (foldView.getVisibility() == View.VISIBLE){
            foldView.setVisibility(View.GONE);
            foldIb.setImageResource(R.drawable.icon_item);
        }else {
            foldView.setVisibility(View.VISIBLE);
            foldIb.setImageResource(R.drawable.icon_item_5);
        }

    }

    @SuppressLint("SetTextI18n")
    public void refreshHomeLimit(){

        homeAmountTv.setText(LnLiAkomapalendmitInfo.instance().getLimitAmount_akomapalend()+" "+ LnLiAkomapalendmitInfo.instance().getCurrency_akomapalend());
        homeTermTv.setText(AppAkomapalendConfigCustomize.instance().getHomeTerm());

        try {
            HttpHelper.inst().fRepayDetail(requireActivity(),this::refreshDetails_akomapalend);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @SuppressLint("SetTextI18n")
    public void refreshDetails_akomapalend(){

        String currency = RpIAkomapalendnfo.instance().getCurrency_akomapalend();

        totalRepaymentTv.setText(RpIAkomapalendnfo.instance().getRepayAmount_akomapalend()+" "+currency);
        dueDateTv.setText(RpIAkomapalendnfo.instance().getRepayDate_akomapalend());
        loanAmountTv.setText(RpIAkomapalendnfo.instance().getAmount_akomapalend()+" "+currency);
        interestTv.setText(RpIAkomapalendnfo.instance().getInterest_akomapalend()+" "+currency);
        daysRemainingTv.setText(RpIAkomapalendnfo.instance().getRemainDay());
        overdueDaysTv.setText(RpIAkomapalendnfo.instance().getOverdueDay_akomapalend());
        overduePenaltyTv.setText(RpIAkomapalendnfo.instance().getOverdueAmount_akomapalend()+" "+currency);

        if (AkomapaLendUtil.isCt()){
            vatView.setVisibility(View.VISIBLE);
            vatTv.setText(RpIAkomapalendnfo.instance().getServiceVatFee_akomapalend()+" "+currency);
        }else vatView.setVisibility(View.GONE);

        if (RpIAkomapalendnfo.instance().isOvdu_akomapalend()) overdueDaysTv.setTextColor(Color.RED);
        else overdueDaysTv.setTextColor(Color.parseColor("#FF666666"));

        if (AkomapaLendUtil.isTsBindAccount()) serviceFeeTv.setText(RpIAkomapalendnfo.instance().getRepayProFee_akomapalend()+" "+currency);
        else serviceFeeTv.setText(RpIAkomapalendnfo.instance().getServiceFee_akomapalend()+" "+currency);

    }

    public void toPay_akomapalend(){
        String path = OtheAkomapalendrsInfo.instance().getRpPath_akomapalend();
        if (!TextUtils.isEmpty(path)) startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(path)));
    }
    
    @Override
    public void onPause() {
        super.onPause();
        HttpHelper.inst().submitEvent("REPAY","END","");
    }

}